<?php
if (isset($_POST['upload_button']) && !empty($_POST['original_cim'])) {
    if (!empty($_POST['hun_cim'])) {
        $hun_cim = $_POST['hun_cim'];
    } else {
        $hun_cim = '';
    }
    $original_cim = $_POST['original_cim'];
    $cast = '<table id="belso">';
    if (!empty($_POST['actor']) || !empty($_POST['role'])) {
        $meret = count($_POST['actor']);
        for ($i = 0; $i < $meret; $i++) {
            if (!isset($_POST['actor'][$i])) {
                $actor[$i] = '<tr><td></td>';
            } else {
                $actor[$i] = '<tr><td>' . $_POST['actor'][$i] . '</td>';
            }
            if (!isset($_POST['role'][$i])) {
                $role[$i] = '<td></td></tr>';
            } else {
                $role[$i] = '<td>' . $_POST['role'][$i] . '</td></tr>';
            }
        }
        for ($i = 0; $i < $meret; $i++) {
            $cast .= $actor[$i] . $role[$i];
        }
    }
    $cast .= '</table>';
    if (!empty($_POST['owner'])) {
        $owner = $_POST['owner'];
    } else {
        $owner = '';
    }
    if (!empty($_POST['make_year'])) {
        $make_year = $_POST['make_year'];
    } else {
        $make_year = '';
    }
    if (!empty($_POST['play_time'])) {
        $hossz = $_POST['play_time'];
    } else {
        $hossz = '';
    }
    if (!empty($_POST['directed_by'])) {
        $directed_by = $_POST['directed_by'];
    } else {
        $directed_by = '';
    }
    if (!empty($_POST['tortenet'])) {
        $tortenet = $_POST['tortenet'];
    } else {
        $tortenet = '';
    }
    if (!empty($_FILES['file']['name'])) {
        $file_name = $_FILES['file']['name'];
    } else {
        $file_name = '';
    }
    require_once 'dbconnect.php';
    $sql = "INSERT INTO movies (hun_title, original_title, year, studio, play_time, director, cast, summary, poster)
        VALUES ('$hun_cim', '$original_cim', '$make_year', '$owner', '$hossz', '$directed_by', '$cast', '$tortenet', '$file_name')";
    if (!mysql_query($sql)) {
        die(mysql_error());
    } else {
        move_uploaded_file($_FILES["file"]["tmp_name"], "kepek/" . $file_name);
        echo 'Sikeres feltöltés';
    }
} else {
    ?>
    <div id="cim">
        Új film felvitele varázsló
    </div>
    <div id="upload_form">
        <form action="index.php?show=insert" method="post" enctype="multipart/form-data">
            <table id="t1">
                <tr>
                    <th scope="row">Magyar cím</th>
                    <td><input type="text" name="hun_cim" id="hun_cim"/></td>
                </tr>
                <tr>
                    <th scope="row">Eredeti cím</th>
                    <td><input type="text" name="original_cim" id="original_cim"/></td>
                </tr>
                <tr>
                    <th scope="row">Év</th>
                    <td><input type="text" name="make_year"/></td>
                </tr>
                <tr>
                    <th scope="row">Stúdió</th>
                    <td><input type="text" name="owner"/></td>
                </tr>
                <tr>
                    <th scope="row">Játékidő</th>
                    <td><input type="text" name="play_time"/></td>
                </tr>
                <tr>
                    <th scope="row">Rendező</th>
                    <td><input type="text" name="directed_by"/></td>
                </tr>
                <tr>
                    <th scope="row">Szereplők <input type="button" id="p" value="+" onclick="add()"/></th>
                    <td>
                        <table id="t2">
                            <thead>
                                <tr>
                                    <th>Színész</th>
                                    <th></th>
                                    <th>Szerep</th>
                                </tr>
                            </thead>
                            <tbody id="new_up">
                                <tr>
                                    <td>
                                        <input type="text" name="actor[]"/>
                                    </td>
                                    <td>-</td>
                                    <td>
                                        <input type="text" name="role[]"/>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Poszter</th>
                    <td>
                        <input type="file" name="file" id="file" onchange="validate_file()"/>
                        <input type="button" id="del" onclick="$('#file').val('')" value="Töröl"/>
                    </td>
                </tr>
                <tr>
                    <th scope="row">Történet</th>
                    <td><textarea rows="10" name="tortenet"></textarea></td>
                </tr>
            </table>
            <input type="submit" name="upload_button" value="Feltölt" id="upload_button"/>
        </form>
    </div>
    <div id="keret"></div>
    <div id="exist"></div>
    <?php
}
?>